package nachos.vm;

import nachos.machine.*;
import java.util.*;

public class Lazy
{

    private int numPg;
    private ArrayList<Integer> pageSOffSet = new ArrayList<Integer>();
    private ArrayList<Integer> pageSNum = new ArrayList<Integer>();
    private int numSecs;
    Coff coff;
    
    Lazy(Coff coff)
    {
        this.coff = coff;
        init();
    }

    private void init()
    {
        for (int s = 0; s < coff.getNumSections(); s++)
        {
            CoffSection section = coff.getSection(s);
            for (int i = 0; i < section.getLength(); i++)
            {
                int vpn = section.getFirstVPN() + i;
                //System.out.println("VPN " + vpn);
                pageSNum.add(vpn, s);
                pageSOffSet.add(vpn, i);
            }

        }
    }

    public TranslationEntry loadSection(int ppn, int vpn)
    {
        if(vpn > pageSNum.size())
            return null;
            
     //Lib.assertTrue(Machine.interrupt().disabled());
        //Lib.debug('v', "LAZY vpn: " + vpn + " ppn: " + ppn);
        CoffSection cSection = coff.getSection(pageSNum.get(vpn));
        TranslationEntry entry = new TranslationEntry(vpn, ppn, true, cSection.isReadOnly(), false, false);
        cSection.loadPage(pageSOffSet.get(vpn), ppn);

        return entry;
    }

    public boolean codeCheck(int vpn)
    {
        return vpn >= 0 && vpn < pageSNum.size();
    }
}
